package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.vo.WxCatalogCurrentVo;
import com.cskaoyan.market.vo.WxCatalogIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxCatalogServiceImpl implements WxCatalogService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;


    @Override
    public WxCatalogIndexVo index() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        MarketCategory marketCategory = new MarketCategory();
        if (marketCategory.getId() != null) {
            criteria.andIdEqualTo(marketCategory.getId());
        }
        if (marketCategory.getName() != null) {
            criteria.andNameEqualTo(marketCategory.getName());
        }

        List<MarketCategory> currentCategory = marketCategoryMapper.selectByExampleSelective(marketCategoryExample, MarketCategory.Column.id, MarketCategory.Column.addTime, MarketCategory.Column.updateTime, MarketCategory.Column.name, MarketCategory.Column.level, MarketCategory.Column.pid, MarketCategory.Column.sortOrder, MarketCategory.Column.picUrl, MarketCategory.Column.desc);

        MarketCategory marketCategory1 = currentCategory.get(0);
        List<MarketCategory> currentBrotherCtory = marketCategoryMapper.selectByExampleSelective(marketCategoryExample, MarketCategory.Column.id, MarketCategory.Column.addTime, MarketCategory.Column.updateTime, MarketCategory.Column.name, MarketCategory.Column.level, MarketCategory.Column.pid, MarketCategory.Column.sortOrder, MarketCategory.Column.picUrl, MarketCategory.Column.keywords, MarketCategory.Column.desc);
        WxCatalogIndexVo wxCatalogVo = new WxCatalogIndexVo();
        wxCatalogVo.setCategoryList(currentCategory);
        wxCatalogVo.setCurrentCategory(marketCategory1);
        wxCatalogVo.setCurrentSubCategory(currentBrotherCtory);


        return wxCatalogVo;
    }

    private Map<Integer, MarketCategory> getCurrentMap(List<MarketCategory> currentCategory) {
        Map<Integer,MarketCategory> map = new HashMap<>();
        for (MarketCategory marketCategory : currentCategory) {
            Integer currentId=marketCategory.getId();
            map.put(currentId,marketCategory);
        }


        return map;
    }

    @Override
    public WxCatalogCurrentVo current(Integer currentId) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        MarketCategory marketCategory = new MarketCategory();
        if (marketCategory.getId() != null) {
            criteria.andIdEqualTo(marketCategory.getId());
        }
        List<MarketCategory> currentCategory = marketCategoryMapper.selectByExampleSelective(marketCategoryExample, MarketCategory.Column.id, MarketCategory.Column.addTime, MarketCategory.Column.updateTime, MarketCategory.Column.name, MarketCategory.Column.level, MarketCategory.Column.pid, MarketCategory.Column.sortOrder, MarketCategory.Column.picUrl, MarketCategory.Column.desc);

        Map<Integer,MarketCategory> getCurrent= getCurrentMap(currentCategory);
        MarketCategory marketCategoryL1 = getCurrent.get(currentId);

        MarketCategoryExample exampleL2 = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria1 = exampleL2.createCriteria();
        criteria1.andPidEqualTo(currentId);

        List<MarketCategory> currentBrotherCtory = marketCategoryMapper.selectByExample(exampleL2);
        WxCatalogCurrentVo wxCatalogVo = new WxCatalogCurrentVo();
        wxCatalogVo.setCurrentCategory(marketCategoryL1);
        wxCatalogVo.setCurrentSubCategory(currentBrotherCtory);

        return wxCatalogVo;
    }
}
